//
//  GenerateQRCodeViewController.m
//  BarCodeAndQRCode
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.

#import "GenerateQRCodeViewController.h"
#import "QRCodeGenerator.h"
#import "AddMemberViewController.h"
@interface GenerateQRCodeViewController ()<UIScrollViewDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation GenerateQRCodeViewController
{
    UIScrollView*mainScroll;
   
    UIButton *time;
    NSString *sn;
 
}
@synthesize mDelegate;
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation];

   

   
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    
    [params setValue:Token forKey:@"tokenId"];
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    
    [HttpRequest requestData:[NSString stringWithFormat:@"Company/%@" ,@"GetReadMemberQrCode"]  params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSLog(@"%@",severDataDict.dataDict);
               
                sn=[severDataDict.dataDict  objectForKey:@"sn"];
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:severDataDict.dataDict forKey:@"qr"];
                [user synchronize];
                
                UIButton* QRCode=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*85/750,ScreenHeight*370/1334, 385/2,385/2) ];
                QRCode.center=self.view.center;
                [self.view addSubview:QRCode];
                
               if ([self.QrCode isEqualToString:@"GetReadMemberQrCode"]) {
                    
                    [self times];
                    [QRCode setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"qrcodeUrl"]]];
                    UIButton* logo=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 30,30) ];
                    [logo setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"logoUrl"]]];
                    logo.center=QRCode.center;
                    [self.view addSubview:logo];
                }else{
                    UIButton* logo=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 30,30) ];
                    [logo setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"logoUrl"]]];
                    logo.center=QRCode.center;
                    [self.view addSubview:logo];
                    [QRCode setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"qrcodeUrl"]]];
                }
                
               
                UILabel* prompt=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-144,ScreenWidth,12)];
                prompt.text=@"对准二维码，即可自动扫描";
                prompt.textColor=zhonghuiColor;
                prompt.textAlignment = NSTextAlignmentCenter;
                
                prompt.font=[UIFont systemFontOfSize:12];
                [self.view addSubview:prompt];
              
                UIButton* qujingkuang=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 550/2,550/2) ];
                [qujingkuang setBackgroundImage:[UIImage imageNamed:@"qujingkuang"] forState:UIControlStateNormal];
                qujingkuang.center=QRCode.center;
                [self.view addSubview:qujingkuang];

       
       
        
    }else{
        [Global hideProgressHUDInternal:self.view];
        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
        [Global showTextHudSub:requestResultStr view:self.view];
    }
     
     }
     else
     {
         [Global hideProgressHUDInternal:self.view];
        
         [Global showTextHudSub:@"连接失败" view:self.view];
     }
     
     }];
    

    
}

- (void)times{

    time = [[UIButton alloc]init];
    time.frame = CGRectMake(ScreenWidth-100,ScreenHeight*35/2200,30,16);
    [time setTitleColor:luseColor forState:UIControlStateNormal];
    [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:time];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownActions:) userInfo:time repeats:YES];
    
    
}
//自动更新
-(void)countDownActions:(NSTimer*)timer{
    
    time = timer.userInfo;
    
    if (!time.superview) {
        [timer invalidate];
        return;
    }
    int currentTime = [[time titleForState:UIControlStateNormal] intValue];
    [time setTitle:[NSString stringWithFormat:@"%d",--currentTime] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont systemFontOfSize:16];
    if (currentTime<1) {
         [time setTitle:[NSString stringWithFormat:@"%d",5] forState:UIControlStateNormal];
        [self times];
        [time removeFromSuperview];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:sn forKey:@"sn"];

        [HttpRequest requestData:@"Company/GetMemberByQrCode" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [mDelegate secondViewController:self headImgUrl:[severDataDict.dataDict objectForKey:@"headImgUrl"] nickName:[severDataDict.dataDict objectForKey:@"nickName"] openID:[severDataDict.dataDict  objectForKey:@"openID"] sex:[NSString stringWithFormat:@"%@",  [severDataDict.dataDict objectForKey:@"sex"]]];
                    [time setTitle:[NSString stringWithFormat:@"%d",10000000] forState:UIControlStateNormal];
                    
                    if ([self.JumpStatus isEqualToString:@"AddView"]) {
                   
                     [self.navigationController popViewControllerAnimated:YES];
                    
                    }else{
                   
                       
                        AddMemberViewController * Login=[[AddMemberViewController alloc]init];
                       
                        [self.navigationController pushViewController:Login animated:YES];

                        
                    }
                    
                    [time removeFromSuperview];
                    [timer invalidate];
                    
            
            
        }else{
        }
         
         }
         else
         {
             [Global hideProgressHUDInternal:self.view];
            
             [timer invalidate];
             [Global showTextHudSub:@"连接失败" view:self.view];
         }
         
         }];
      
        
        
        
        
        
        
    }
}

-(void)cancel:(UIButton *)sender{
  [time removeFromSuperview];
 [self.navigationController popViewControllerAnimated:YES];


}
-(void)BackAction:(UIButton *)sender{
    [time removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setNavigation{
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.title = @"扫公众号二维码";
    if ([self.QrCode isEqualToString:@"GetReadMemberQrCode"]) {
        
        UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, ScreenWidth*65/750, 30)];
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        
        [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cancel.titleLabel.font=[UIFont systemFontOfSize:contenfot];
        [cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:cancel];
        self.navigationItem.rightBarButtonItem = myBtnBtn;

    }else{

        
    }
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}



@end
